#pragma once

#include "dfize.h"
#include "CPickSceneNode.h"
#include "CTile.h"
#include "CBuilding.h"

class CRegionSceneNode;
struct SResource;
class CBlockSceneNode;

//#define USE_IRR_MAP

class CBlockSceneNode : public ISceneNode
{
public:
	CBlockSceneNode(ISceneNode *parent=0, ISceneManager *mgr=0, s32 id=-1, CRegionSceneNode *region=0, const core::vector3di& globalposition = vector3di(0,0,0), const core::vector3df& position = core::vector3df(0,0,0), const core::vector3df& rotation = core::vector3df(0,0,0), const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f));
	~CBlockSceneNode();

	virtual void render();
	virtual void renderBuildings();

	virtual const core::aabbox3d<f32>& getBoundingBox() const
    {
		return BoundingBox;
	}

	virtual const aabbox3df getTransformedBoundingBox() const
	{
		return TransformedBoundingBox;
	}

	void OnAnimate(u32 time)
	{
		ISceneNode::OnAnimate(time);
	}

	void OnRegisterSceneNode()
	{
		ISceneNode::OnRegisterSceneNode();
	}

	void removeTile(u32 i)
	{
	//	delete Tiles[i];
//		Tiles.erase(i);
	//	getTile(i).Removed = true;
	//	getTile(i).Visible = false;
	}
#ifdef USE_IRR_MAP
#else
	void showTile(u32 i, bool show=true)
	{
		Tiles[i]->Visible = show;
	}
#endif

	void showTile(CTile *tile, bool show=true);

	void setTileType(u32 i, u16 tiletype);

	CTile* getTileFromLine(line3df line);
	bool intersectsWithBox(aabbox3df box, bool accurate=false);

	bool isVisible();

#ifdef USE_IRR_MAP
#else
	CTile* getTile(u32 i) { return Tiles[i]; }
	CTile* getTile(s32 x, s32 y) const;
#endif
	u32 getTileCount() { return Tiles.size(); }

	CRegionSceneNode* getRegion() { return Region; }

	void addTile(vector3df position, u16 tiletype, SBasicMaterial *m);

	void addBuilding(vector3df position, u32 type, SBasicMaterial *m);

	void optimizeCollisionData(bool reset=false);

	const vector3di& getGlobalPosition() const { return GlobalPosition; }

	void updateTiles();
	bool areTilesUpdated() const { return TilesUpdated; }

	u32 getHiddenTileCount() const { return HiddenTileCount; }

protected:

	u32 HiddenTileCount;

	bool TilesUpdated;

	aabbox3df BoundingBox, TransformedBoundingBox;

	vector3di GlobalPosition;

	CRegionSceneNode *Region;
#ifdef USE_IRR_MAP
	core::map<vector2di, CTile*> Tiles;
#else
	array<CTile*> Tiles;
	array<CBuilding*> Buildings;
#endif
};